﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using tmAsk.Common;

namespace tmAsk.Manager
{
    public partial class Admin_add : System.Web.UI.Page
    {
        function make_htm = new function();//继承类
        tmAsk.Data.MakehtmlSQL SQL = new tmAsk.Data.MakehtmlSQL();
        protected void Page_Load(object sender, EventArgs e)
        {
            function.CheckAdminLogin();
            string T = Request["T"].Trim().Replace("'", "`");

            switch (T)
            {
                case "b_config":
                    T_B_config();
                    break;
                case "b_info":
                    T_B_info();
                    break;
                case "b_msg":
                    T_B_msg();
                    break;
                case "b_askClass":
                    T_B_askClass();
                    break;
                case "b_userLevel":
                    T_b_userLevel();
                    break;
                case "b_user":
                    T_B_user();
                    break;
                case "b_admin":
                    T_B_admin();
                    break;
                case "Collect":
                    T_B_Collect();
                    break;
                case "b_news":
                    T_B_news();
                    break;
                case "b_helpClass":
                    T_B_helpClass();
                    break;
                case "b_help":
                    T_B_help();
                    break;
                case "b_links":
                    T_B_links();
                    break;
            }
        }

        /// <summary>
        /// 网站基本信息设置
        /// </summary>
        private void T_B_config()
        {
            string c_title = Request.Form["c_title"].Trim();
            string c_url = Request.Form["c_url"].Trim();
            string c_ischeck = Request.Form["c_ischeck"].Trim();
            string c_isVisitors = Request.Form["c_isVisitors"].Trim();
            string c_defaultpoint = Request.Form["c_defaultpoint"].Trim();
            string c_settime = Request.Form["c_settime"].Trim();
            string c_count = Request.Form["c_count"].Trim();
            string c_template = Request.Form["c_template"].Trim();
            string c_askupload = Request.Form["c_askupload"].Trim();
            string c_reupload = Request.Form["c_reupload"].Trim();
            string c_ad = Request.Form["c_ad"];

            if (!function.CheckNull(c_title, c_url, c_ischeck, c_isVisitors, c_defaultpoint, c_settime))
            {
                Response.Write(function.alert_text(4, "更新失败，请将资料填写完整！", ""));
                return;
            }
            try
            {
                function.isNumber(c_ischeck, c_isVisitors, c_defaultpoint, c_settime);
            }
            catch (Exception)
            {
                Response.Write(function.alert_text(4, "添加失败，无法转换成数字类型！", ""));
                return;
            }
            string ad_url = Context.Server.MapPath("/js/ad.js");

            make_htm.writehtml(ad_url, c_ad);//写入JS代码

            string sql = "update B_config set c_ad = '" + c_ad.Replace("'", "") + "', c_title = '" + c_title + "',c_url = '" + c_url + "',c_ischeck = " + c_ischeck + ",c_defaultpoint =" + c_defaultpoint + ",c_settime = '" + c_settime + "',c_count='" + c_count + "',c_isVisitors=" + c_isVisitors + ",c_template='" + c_template + "',c_askupload=" + c_askupload + ",c_reupload=" + c_reupload + "";
            int i = SQL.ExecuteSql(sql);
            if (i > 0)
            {
                Response.Write(function.alert_text(4, "更新成功", ""));
            }
            else
            {
                Response.Write(function.alert_text(4, "更新失败", ""));
            }
        }

        /// <summary>
        /// 基本信息设置
        /// </summary>
        private void T_B_info()
        {
            string i_title = Request.Form["i_title"].Trim();
            string i_content = Request.Form["i_content"].Trim();
            string i_time = DateTime.Now.ToString();
            string i_model = Request.Form["i_url"].Trim();
            if (!function.CheckNull(i_title, i_content, i_model))
            {
                Response.Write(function.alert_text(4, "添加失败，请将资料填写完整！", ""));
                return;
            }
            string sql = "insert into B_info(i_title,i_content,i_time,i_model) values('" + i_title + "','" + i_content + "','" + i_time + "','" + i_model + "')";
            int i = SQL.ExecuteSql(sql);
            if (i > 0)
            {
                Response.Write(function.alert_text(2, "添加成功", "admin_url.aspx?template=template/t_admin_info.html"));
            }
            else
            {
                Response.Write(function.alert_text(4, "添加失败", ""));
            }
        }

        /// <summary>
        /// 站内短信管理
        /// </summary>
        private void T_B_msg()
        {
            string m_type = Request.Form["m_type"].Trim().Replace("'", "`");
            string u_nickName = Request.Form["u_nickName"].Trim().Replace("'", "`");//收信人昵称
            string m_content = Request.Form["m_content"].Trim().Replace("'", "`");
            string m_time = DateTime.Now.ToString();
            if (!function.CheckNull(m_content))
            {
                Response.Write(function.alert_text(4, "添加失败，请将资料填写完整！", ""));
                return;
            }
            try
            {
                function.isNumber(m_type);
            }
            catch (Exception)
            {
                Response.Write(function.alert_text(4, "添加失败，无法转换成数字类型！", ""));
                return;
            }
            string sql = string.Empty;
            if (u_nickName.Equals(""))
            {
                //给所有用户发信息
                DataTable allUsre = SQL.GetList("B_user", "1=1", "u_id").Tables[0];
                if (allUsre.Rows.Count > 0)
                {
                    for (int n = 0; n < allUsre.Rows.Count; n++)
                    {
                        sql = "insert into B_msg(m_type,m_acceptuser,m_content,m_time) values(" + m_type + "," + allUsre.Rows[n]["u_id"] + ",'" + m_content + "','" + m_time + "');";
                        int i = SQL.ExecuteSql(sql);
                        if (i <= 0)
                        {
                            Response.Write(function.alert_text(4, "添加失败", ""));
                            break;
                        }
                    }

                    Response.Write(function.alert_text(2, "发送成功", "admin_url.aspx?template=template/t_admin_msg.html"));
                }
            }
            else
            {
                DataTable dt = SQL.GetList("B_user", "u_nickName='" + u_nickName + "'", "u_id").Tables[0];
                if (dt.Rows.Count <= 0)
                {
                    //没找到
                    Response.Write(function.alert_text(4, "此用户不存在，请重新输入昵称", ""));
                    return;
                }
                int m_acceptuser = 0;
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    m_acceptuser = Convert.ToInt32(dt.Rows[j]["u_id"]);
                }
                sql = "insert into B_msg(m_type,m_acceptuser,m_content,m_time) values(" + m_type + "," + m_acceptuser + ",'" + m_content + "','" + m_time + "')";
                int k = SQL.ExecuteSql(sql);
                if (k > 0)
                {
                    Response.Write(function.alert_text(2, "发送成功", "admin_url.aspx?template=template/t_admin_msg.html"));
                }
                else
                {
                    Response.Write(function.alert_text(4, "添加失败", ""));
                }
            }
        }

        /// <summary>
        /// 问题分类设置
        /// </summary>
        private void T_B_askClass()
        {
            string a_className = Request.Form["a_className"].Trim().Replace("'", "`");
            string a_classLevel = Request.Form["a_classLevel"].Trim().Replace("'", "`");
            string a_parentid = "-1";
            string a_askCount = "0";
            if (!function.CheckNull(a_className))
            {
                Response.Write(function.alert_text(4, "添加失败，请将资料填写完整！*号为必填项。", ""));
                return;
            }
            try
            {
                function.isNumber(a_classLevel, a_parentid);
            }
            catch (Exception)
            {
                Response.Write(function.alert_text(4, "添加失败，无法转换成数字类型！", ""));
                return;
            }
            if (a_classLevel == "1")
            {
                a_parentid = "0";
            }
            else
            {
                a_parentid = Request.Form["haveParent"];
            }

            if (a_parentid == "" || a_parentid == null)
            {
                Response.Write(function.alert_text(4, "请保证数据库中有一个以上的一级分类！", ""));
                return;
            }
            string sql = "insert into B_askClass(a_className,a_parentid,a_classLevel,a_askCount) values('" + a_className + "'," + a_parentid + "," + a_classLevel + "," + a_askCount + ")";
            int i = SQL.ExecuteSql(sql);
            if (i > 0)
            {
                Response.Write(function.alert_text(2, "添加成功", "admin_url.aspx?template=template/t_admin_class_ask.html"));
            }
            else
            {
                Response.Write(function.alert_text(4, "添加失败", ""));
            }
        }

        /// <summary>
        /// 会员等级设置
        /// </summary>
        private void T_b_userLevel()
        {
            string u_title = Request.Form["u_title"].Trim().Replace("'", "`");
            string u_level = Request.Form["u_level"].Trim().Replace("'", "`");
            string u_point = Request.Form["u_point"].Trim().Replace("'", "`");
            if (!function.CheckNull(u_title, u_level, u_point))
            {
                Response.Write(function.alert_text(4, "添加失败，请将资料填写完整！*号为必填项。", ""));
                return;
            }
            try
            {
                function.isNumber(u_level, u_point);
            }
            catch (Exception)
            {
                Response.Write(function.alert_text(4, "添加失败，无法转换成数字类型！", ""));
                return;
            }
            string sql = "insert into b_userLevel(u_title,u_level,u_point) values('" + u_title + "'," + u_level + "," + u_point + ")";
            int i = SQL.ExecuteSql(sql);
            if (i > 0)
            {
                Response.Write(function.alert_text(2, "添加成功", "admin_url.aspx?template=template/t_admin_user_level.html"));
            }
            else
            {
                Response.Write(function.alert_text(4, "添加失败", ""));
            }
        }

        /// <summary>
        /// 注册会员管理
        /// </summary>
        private void T_B_user()
        {
            function make_htm = new function();//继承类
            string u_email = Request.Form["u_email"].Trim().Replace("'", "`");
            string u_phone = Request.Form["u_phone"].Trim().Replace("'", "`");
            string u_nickName = Request.Form["u_nickName"].Trim().Replace("'", "`");
            string u_passWord = Request.Form["u_passWord"].Trim().Replace("'", "");
            string repeatPassWord = Request.Form["repeatPassWord"].Trim().Replace("'", "");
            string u_regTime = DateTime.Now.ToString();
            string u_img = alldata.Config_defaultimg;
            //判断页面文本框是否为空
            if (!function.CheckNull(u_email, u_phone, u_nickName, u_passWord, repeatPassWord))
            {
                Response.Write(function.alert_text(4, "添加失败，请将资料填写完整！*号为必填项。", ""));
                return;
            }
            //判断两次密码是否一致
            if (!u_passWord.Equals(repeatPassWord))
            {
                Response.Write(function.alert_text(4, "两次密码不一致，请重新输入！", ""));
                return;
            }
            //判断用户名是否存在
            DataTable dt = SQL.GetList("B_user", "u_email='" + u_email + "'", "u_email").Tables[0];
            if (dt.Rows.Count > 0)
            {
                Response.Write(function.alert_text(4, "此用户名已存在，请重新输入！", ""));
                return;
            }
            //SQL.ExecuteSql("insert into B_image(i_title,i_path,i_url,i_model) values('初始头像','" + u_img + "','','头像')");
            string sql = "insert into B_user(u_email,u_passWord,u_phone,u_nickName,u_askNumber,u_reviewNumber,u_goodNumber,u_regTime,u_point,u_img,u_level,u_ischeck) values('" + u_email + "','" + make_htm.Md5(u_passWord) + "','" + u_phone + "','" + u_nickName + "'" + ",0,0,0,'" + u_regTime + "',0,'" + u_img + "',1,0)";
            int i = SQL.ExecuteSql(sql);
            if (i > 0)
            {
                Response.Write(function.alert_text(2, "添加成功", "admin_url.aspx?template=template/t_admin_user.html"));
            }
            else
            {
                Response.Write(function.alert_text(4, "添加失败", ""));
            }
        }

        /// <summary>
        /// 管理员设置
        /// </summary>
        private void T_B_admin()
        {
            string a_username = Request.Form["a_username"].Trim().Replace("'", "");
            string a_userpassword = Request.Form["a_userpassword"].Trim().Replace("'", "`");
            if (!function.CheckNull(a_username, a_userpassword))
            {
                Response.Write(function.alert_text(4, "添加失败，请将资料填写完整！*号为必填项。", ""));
                return;
            }
            DataTable dt = SQL.GetList("B_admin", "a_username='" + a_username + "'", "a_username").Tables[0];
            if (dt.Rows.Count > 0)
            {
                Response.Write(function.alert_text(4, "此用户名已存在，请重新输入！", ""));
                return;
            }

            a_userpassword = make_htm.Md5(a_userpassword);
            string sql = "insert into B_admin(a_username,a_userpassword) values('" + a_username + "','" + a_userpassword + "')";
            int i = SQL.ExecuteSql(sql);
            if (i > 0)
            {
                Response.Write(function.alert_text(2, "添加成功", "admin_url.aspx?template=template/t_admin_administrator.html"));
            }
            else
            {
                Response.Write(function.alert_text(4, "添加失败", ""));
            }
        }

        private void T_B_Collect()
        {
            if ("del" == Request.Form["target"].ToString())
            {
                string id = Request.Form["Cid"];
                if (id == null || id == "")
                {
                    Response.Write(function.alert_text(4, "尚未选择数据！", ""));
                    return;
                }
                string[] delid = id.Split(',');
                for (int i = 0; i < delid.Length; i++)
                {
                    SQL.ExecuteSql("delete from b_api_data where a_id=" + delid[i]);
                }
                Response.Redirect("admin_url.aspx?template=template/t_admin_api.html");
            }
            else
            {
                string aid = Request.Form["Cid"];
                string Uname = Request.Form["uid"].Trim();
                string Classid = Request.Form["classid"];
                int Uid = 0;
                int askid = 0;
                if (Uname == "")
                {
                    Response.Write(function.alert_text(4, "请指定用户名！", ""));
                    return;
                }
                if (!string.IsNullOrEmpty(aid))
                {
                    //提取会员ID
                    DataTable User = SQL.GetList(1, "B_user", "u_id='" + Uname + "'", "u_id asc").Tables[0];
                    if (User.Rows.Count > 0)
                    {
                        Uid = Convert.ToInt16(User.Rows[0]["u_id"]);
                        string[] cid = aid.Split(',');
                        for (int i = 0; i < cid.Length; i++)
                        {
                            DataTable rs = SQL.GetList("select * from B_api_data where a_id =" + cid[i].ToString()).Tables[0];
                            if (rs.Rows.Count > 0)
                            {
                                try
                                {
                                    string sqlstring = "insert into b_ask(a_classid,a_title,a_content,u_id,a_reviewNumber,a_time,a_state,a_point,a_hitNumber,a_keyword,a_ischeck,a_delete,a_hiddenuser,a_recommend,a_classLevel) values(" + Classid + ",'" + rs.Rows[0]["A_title"].ToString() + "','" + function.HTMLEncode(rs.Rows[0]["A_content"].ToString()) + "'," + Uid + ",1,'" + rs.Rows[0]["A_addtime"].ToString() + "',0,1,1,'" + rs.Rows[0]["A_key"].ToString() + "',0,0,0,0,0)";
                                    //Response.Write(sqlstring+"<br />");
                                    askid = SQL.Add(sqlstring);
                                    // Response.Write(function.alert_text(2, askid.ToString(), "admin_url.aspx?template=template/t_admin_api.html"));

                                    if (function.HTMLEncode(rs.Rows[0]["A_reply"].ToString()) != "")
                                    {
                                        string sql = "insert into B_review(r_content,r_time,a_id,r_isGood,r_agreeNumber,r_unagreeNumber,r_userid,r_hiddenuser) values('" + function.HTMLEncode(rs.Rows[0]["A_reply"].ToString()) + "','" + rs.Rows[0]["A_replyTime"].ToString() + "'," + askid + ",0,0,0," + Uid + ",1)";
                                        SQL.ExecuteSql(sql);
                                    }
                                    SQL.ExecuteSql("delete from B_api_data where a_id =" + cid[i].ToString());
                                }
                                catch (Exception e)
                                {
                                    Response.Write(e.Message);
                                }
                            }
                        }

                        Response.Write(function.alert_text(2, "入库成功!", "admin_url.aspx?template=template/t_admin_api.html"));
                    }
                    else
                    {
                        Response.Write(function.alert_text(4, "该会员不存在", ""));
                    }
                }
                else
                {
                    Response.Write(function.alert_text(4, "没有选择任何项", ""));
                }
            }
        }

        /// <summary>
        /// 添加公告
        /// </summary>
        private void T_B_news()
        {
            string n_title = Request.Form["n_title"].Trim().Replace("'", "`");
            string n_content = Request.Form["n_content"];
            string n_time = DateTime.Now.ToString();

            if (!function.CheckNull(n_title, n_content))
            {
                Response.Write(function.alert_text(4, "请将资料填写完整！", ""));
                return;
            }
            string sql = "insert into B_news(n_title,n_content,n_time,n_hits) values('" +
                n_title + "','" + n_content + "','" + n_time + "',0)";
            int i = SQL.ExecuteSql(sql);
            if (i > 0)
            {
                Response.Write(function.alert_text(2, "添加成功", "admin_url.aspx?template=template/t_admin_news.html"));
            }
            else
            {
                Response.Write(function.alert_text(4, "添加失败", ""));
            }
        }

        /// <summary>
        /// 添加帮助分类
        /// </summary>
        private void T_B_helpClass()
        {
            string h_className = Request.Form["h_className"].Trim().Replace("'", "`");
            if (!function.CheckNull(h_className))
            {
                Response.Write(function.alert_text(4, "请将资料填写完整！", ""));
                return;
            }
            string sql = "insert into B_helpClass(h_className) values('" + h_className + "')";
            int i = SQL.ExecuteSql(sql);
            if (i > 0)
            {
                Response.Write(function.alert_text(2, "添加成功", "admin_url.aspx?template=template/t_admin_class_help.html"));
            }
            else
            {
                Response.Write(function.alert_text(4, "添加失败", ""));
            }
        }

        /// <summary>
        /// 添加帮助信息
        /// </summary>
        private void T_B_help()
        {
            string h_title = Request.Form["h_title"].Trim().Replace("'", "`");
            string h_content = Request.Form["h_content"].Trim();
            string h_classid = Request.Form["h_classid"];

            if (!function.CheckNull(h_title, h_content))
            {
                Response.Write(function.alert_text(4, "请将资料填写完整！", ""));
                return;
            }
            string sql = "insert into B_help(h_title,h_content,h_hits,h_classid ) values('" +
                h_title + "','" + h_content + "',0," + h_classid + ")";
            int i = SQL.ExecuteSql(sql);
            if (i > 0)
            {
                Response.Write(function.alert_text(2, "添加成功", "admin_url.aspx?template=template/t_admin_help.html"));
            }
            else
            {
                Response.Write(function.alert_text(4, "添加失败", ""));
            }
        }

        private void T_B_links()
        {
            string l_name = Request.Form["l_name"].Trim().Replace("'", "`");
            string l_url = Request.Form["l_url"].Trim();
            string l_img = "";
            string l_order = Request.Form["l_order"].Trim();
            string l_type = "1";
            if (function.isNumber(l_order))
            {
                Response.Write(function.alert_text(4, "排序只能填写数字！", ""));
                return;
            }
            #region
            HttpPostedFile userPostedFile = Request.Files["l_img"];
            //返回0没有选择文件
            //返回1文件太大
            //返回2文件格式有问题
            //返回3图片文件非法
            if (userPostedFile != null)
            {
                if (userPostedFile.ContentLength > 0)
                {
                    l_img = make_htm.Upload("../uploadfiles/link/", 450, 200, userPostedFile, "jpg,gif,JPG,GIF");
                    if (l_img == "0")
                    {
                        Response.Write(function.alert_text(4, "添加失败，没有选择文件", ""));
                        return;
                    }
                    else if (l_img == "1")
                    {
                        Response.Write(function.alert_text(4, "添加失败，图片不能超过200K！", ""));
                        return;
                    }
                    else if (l_img == "2")
                    {
                        Response.Write(function.alert_text(4, "添加失败，文件格式有问题！", ""));
                        return;
                    }
                    else if (l_img == "3")
                    {
                        Response.Write(function.alert_text(4, "添加失败，图片文件非法！", ""));
                        return;
                    }
                    else if (l_img == "-3")
                    {
                        Response.Write(function.alert_text(4, "添加失败", ""));
                        return;
                    }
                }
            }
            #endregion
            if (l_img == "")
            {
                l_type = "0";
            }
            string sql = "insert into B_links(l_name,l_url,l_img,l_order,l_type) values('" + l_name + "','" + l_url + "','" + l_img + "','" + l_order + "','" + l_type + "')";
            int j = SQL.ExecuteSql(sql);
            if (j <= 0)
            {
                Response.Write(function.alert_text(4, "修改失败！", ""));
                return;
            }
            Response.Redirect("admin_url.aspx?template=template/t_admin_links.html");
        }
    }
}